<?php 
/*
 * 自定义全局公共方法
 */
//定义全局常量
function Init()
{
	define('IS_POST',Yii::$app->request->isPost);
	define('IS_GET',Yii::$app->request->isGet);
}
// 获取查询对象
function S($table='')
{
	return (new common\models\M($table))->createQuery();
}
// 获取增，删，改对象
function M($table='')
{
	return new common\models\M($table);
}
/**
* 数据添加 
* @param    obj query对象
* @param    obj page_num 每页显示条数
*/
function page($obj,$page_num)
{
	return  common\models\M::page($obj,$page_num);
}
//文件上传
function upload($filename,&$savePath='')
{
	return common\controllers\C::upload($filename,$savePath);
}
function post($data='',$params='')
{
	$request=Yii::$app->request;
	return empty($data)?$request->post():$request->post($data,$params='');
}
function get($data='',$params='')
{
	$request=Yii::$app->request;
	return empty($data)?$request->get():$request->get($data,$params);
}
function cookieAdd($name,$value)
{
	return Yii::$app->response->cookies->add(new yii\web\Cookie(['name'=>$name,'value'=>$value]));
}
function cookieDel($name)
{
	return Yii::$app->response->cookies->remove($name);
}
function cookieGet($name)
{
	$cookie=Yii::$app->request->cookies->get($name);
 	return  ($cookie!== null) ? $cookie->value: null;
}

function sessionStart() 
{
	return Yii::$app->session->open();
}
function sessionClose() 
{
	return Yii::$app->session->close();
}
function sessionAdd($name,$value) 
{
	return Yii::$app->session->set($name,$value);
}
function sessionGet($name) 
{
	return Yii::$app->session->get($name);
}
function sessionDel($name) 
{
	return Yii::$app->session->remove($name);
}
function query($sql='')
{
	return \Yii::$app->db->createCommand($sql);
}

/**
 * @param string $api_conf
 * @param string $api_host
 * @return string
 * 获取接口url
 */
 function getApiRequestUrl( $api_conf = '' , $api_host = '' ){

    //接口的域名
    if( $api_host == '' ){
        $api_host = actionGetparam('API_HOST');
    }

    //获取接口的配置
    $api_conf_arr =actionGetparam('API_CONF');
    if( !empty( $api_conf_arr[$api_conf] ) ){
        $api_conf_str = $api_conf_arr[$api_conf];
    }else{
        $api_conf_str = '';
    }

    return $api_host .'/'. $api_conf_str;

}

/**
 * @param $config
 * 从配置项中取值
 */
 function actionGetparam($config){
    return  \Yii::$app->params[$config];

}

/**
 * CurlPost 方法
 * 用于请求外部接口
 * @param $url 请求的路径
 * @param $data 请求的参数
 * @param $timeout
 *
 */
function CurlPost($url, $param = null, $timeout = 10 ) {

    //初始化curl
    $curl = curl_init();

    curl_setopt($curl, CURLOPT_URL, $url); // 设置请求的路径
    curl_setopt($curl, CURLOPT_POST, 1); //设置POST提交
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1 ); //显示输出结果
    curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);

    //提交数据
    if (is_array($param)) {
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($param));
    } else {
        curl_setopt($curl, CURLOPT_POSTFIELDS, $param);
    }

    //执行请求
    $data = $data_str = curl_exec($curl);
    //处理错误
    if ($error = curl_error($curl)) {
        $logdata = array(
            'url' => $url,
            'param' => $param,
            'error' => '<span style="color:red;font-weight: bold">' . $error . '</span>',
        );

    }


    curl_close($curl);

    //json数据转换为数组
    $data = json_decode( $data , true );
    if( !is_array( $data ) ){
        $data = $data_str;
    }
    return $data;

}
?>